package com.jzr.board.base

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jzr.board.R
import com.jzr.board.bean.CallBean
import com.jzr.board.bean.CareLabelListBean
import com.jzr.board.ui.adapter.CareLabelAdapter
import com.jzr.board.view.CustomDialog
import com.jzr.board.view.LoadingDialog
import com.jzr.board.view.SwipeBackActivity.SwipeBackActivity
import kotlinx.android.synthetic.main.dialog_carelabel.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe

abstract class BaseActivity : SwipeBackActivity() {

    private var loadingDialog: LoadingDialog? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //取消状态栏
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
        val params: WindowManager.LayoutParams = window.attributes
        params.systemUiVisibility =
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE
        window.attributes = params
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(getLayoutId())
        attachView()
        initView()
        if(!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this)
        }

    }

    @Subscribe
    fun getEvenBusMessage(baseMessage:BaseEvenBus){
        when(baseMessage.messageId){
            BaseEvenBus.CMD->{
                showCallDialog(baseMessage.callBean)
            }
        }
    }

private var  callDialog : CustomDialog? = null

    /**
  呼叫dialog
     */
    private fun showCallDialog(data: CallBean?) {
        hideCallDialog()

        val view = View.inflate(BaseApplication.getContext(), R.layout.dialog_call, null)
        val tvName = view.findViewById<TextView>(R.id.tvName)
        val tvBedNo = view.findViewById<TextView>(R.id.tvBedNo)
        val tvCureNo = view.findViewById<TextView>(R.id.tvCureNo)
        val ivDelete = view.findViewById<ImageView>(R.id.ivDelete)

        tvName.text = data?.data?.name
        tvBedNo.text = data?.data?.bedNo
        tvCureNo.text = data?.data?.cureNo

        ivDelete.setOnClickListener {
            hideCallDialog()
        }

        callDialog = CustomDialog(this, 500, 500, view, R.style.MyDialog)
        callDialog?.setCancelable(true)
        callDialog?.show()

    }

    private  fun hideCallDialog(){
        if(callDialog!= null && callDialog?.isShowing!!){
            callDialog?.dismiss()
            callDialog = null
        }
    }



    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) {
            window.decorView.systemUiVisibility =
                (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        detachView()
        EventBus.getDefault().unregister(this)
    }

    private fun finishActivity() {
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out)
    }

    fun startActivityIn(
        intent: Intent?,
        curAct: Activity
    ) {
        if (intent != null) {
            curAct.startActivity(intent)
            curAct.overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out)
        }
    }

    override fun finish() {
        super.finish()
        finishActivity()
    }

    open fun showLoadingDialog(text :String?) {
        dismissLoadingDialog()
        val builder:LoadingDialog.Builder =
          LoadingDialog.Builder(this)
                .setMessage(text)
                .setCancelable(true)
        loadingDialog = builder.create()
        loadingDialog?.show()
    }

    open fun dismissLoadingDialog() {
        if (null != loadingDialog && loadingDialog!!.isShowing) {
            loadingDialog?.dismiss()
            loadingDialog = null
        }
    }

    abstract fun getLayoutId(): Int

    abstract fun attachView()

    abstract fun detachView()

    abstract fun initView()


}